<template>
	<div>
		<el-card class="box-card">
			<div class="Serach_b">
				<el-input v-model="fy.uid" type="input" autosize placeholder="请输入用户ID" @keyup.enter.native="SearchData"
					style="width: 300px;margin-left: 20px;" />
				<el-button type="primary" icon="el-icon-search" @click="SearchData()">搜索</el-button>
				<!-- <el-button type="primary" @click="handleExport()">导出</el-button> -->
			</div>
			<el-table :data="CartList" border>
				<el-table-column prop="memberId" label="用户ID" />
				<el-table-column prop="productId" label="类型">
					<template slot-scope="scope">
						<span v-if="scope.row.typeId!=3">
							{{ scope.row.typeId == 1 ? "收入" : "支出" }}</span>
						<span v-else>已退回</span>
					</template>
				</el-table-column>
				<el-table-column prop="nickName" label="用户名称" />
				<el-table-column prop="balance" label="数量" />
				<el-table-column prop="profit" label="余额" />
				<el-table-column prop="createTime" label="创建时间" />
				<el-table-column prop="detailType" label="描述" />
				<el-table-column prop="remark" label="详情" />
				<!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="DelClick(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column> -->
			</el-table>
			<el-pagination :page-sizes="[10, 15]" :page-size="fy.currentCount"
				layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
				@current-change="handleCurrentChange" />
		</el-card>
	</div>
</template>

<script>
	import * as syStoreCartApi from '@/api/Add'
	import * as StoreCartApi from '@/api/Search'
	import {
		timestampToTime
	} from '@/utils/date'
	// import { parseTime } from "@/utils/index";
	export default {
		data() {
			return {
				timestampToTime,
				fy: {
					currentCount: 10,
					currentPage: 1
				},
				total: 0,
				CartList: []
			}
		},
		created() {
			this.SearchData()
		},
		methods: {
			dateArrayTransfer(dateArray) {
				if (dateArray == null || dateArray == '') {
					return '无';
				}
				if (dateArray.length == 6) {
					var returnDate = dateArray[0] + "-" +
						this.returnDoubleNum(dateArray[1]) + "-" +
						this.returnDoubleNum(dateArray[2]) + " " +
						this.returnDoubleNum(dateArray[3]) + ":" +
						this.returnDoubleNum(dateArray[4]) + ":" +
						this.returnDoubleNum(dateArray[5]);
				} else {
					var returnDate = dateArray[0] + "-" +
						this.returnDoubleNum(dateArray[1]) + "-" +
						this.returnDoubleNum(dateArray[2]) + " " +
						this.returnDoubleNum(dateArray[3]) + ":" +
						this.returnDoubleNum(dateArray[4]) + ":00";
				}
				return returnDate;
			},
			returnDoubleNum(number) {
				return (Array(2).join(0) + number).slice(-2); //创建一个长度为2的数组，且默认用0填充；然后用传过来的数添加都右边，然后从右向左截取两位！
			},
			DelClick(row) {
				// syStoreCartApi.DelStoreCart({ idList: row.id }).then((res) => {
				//   if (res.data == true) {
				//     this.dialogVisible = false;
				//     this.SearchData();
				//     this.$notify({
				//       title: "成功",
				//       message: "删除成功",
				//       type: "success",
				//     });
				//   } else {
				//     this.dialogVisible = false;
				//     this.SearchData();
				//     this.$notify({
				//       title: "失败",
				//       message: "删除失败",
				//       type: "error",
				//     });
				//   }
				// });
			},
			SearchData() {
				StoreCartApi.selCrmAccountQueryBrief({
					...this.fy,
					accountType: '2'
				}).then((res) => {
					this.total = res.data.totalRecord
					this.CartList = res.data.list
					this.CartList.map(item => {
						item.createTime = this.dateArrayTransfer(item.createTime)
					})
					// this.CartList.forEach((item) => {
					// 	item.createTime = this.parseTime(item.createTime)
					// })
				})
			},
			handleSizeChange(value) {
				this.fy.currentCount = value
				this.SearchData()
			},
			handleCurrentChange(value) {
				this.fy.currentPage = value
				this.SearchData()
			},
			handleExport() {
				StoreCartApi.getTypeAllCrmAccount({
					type: '2'
				}).then((res) => {
					import('@/vendor/Export2Excel').then((excel) => {
						// 设置导出表格的头部
						const tHeader = [
							'用户ID',
							'类型',
							'用户名称',
							'数量',
							'余额',
							'创建时间',
							'描述'
						]
						// 设置要导出的属性
						const filterVal = [
							'id',
							'productId',
							'balance',
							'profit',
							'createTime',
							'detailType'
						]
						// 获取当前展示的表格数据
						const list = res.data.map((k) => {
							return {
								...k,
								productId: k.productId == 1 ? '收入' : '支出'
							}
						})
						// 将要导出的数据进行一个过滤
						const data = this.formatJson(filterVal, list)
						// 调用我们封装好的方法进行导出Excel
						excel.export_json_to_excel({
							// 导出的头部
							header: tHeader,
							// 导出的内容
							data,
							// 导出的文件名称
							filename: '鸡蛋变动记录',
							// 导出的表格宽度是否自动
							autoWidth: this.autoWidth,
							// 导出文件的后缀类型
							bookType: this.bookType
						})
					})
				})
			},
			// 对要导出的内容进行过滤
			formatJson(filterVal, jsonData) {
				return jsonData.map((v) =>
					filterVal.map((j) => {
						if (j === 'createTime') {
							return this.parseTime(v[j])
						} else {
							return v[j]
						}
					})
				)
			},
			// 过滤时间
			parseTime(time, cFormat) {
				if (arguments.length === 0) {
					return null
				}
				const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
				let date
				if (typeof time === 'object') {
					date = time
				} else {
					if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
						time = parseInt(time)
					}
					if (typeof time === 'number' && time.toString().length === 10) {
						time = time * 1000
					}
					date = new Date(time)
				}
				const formatObj = {
					y: date.getFullYear(),
					m: date.getMonth() + 1,
					d: date.getDate(),
					h: date.getHours(),
					i: date.getMinutes(),
					s: date.getSeconds(),
					a: date.getDay()
				}
				const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
					let value = formatObj[key]
					// Note: getDay() returns 0 on Sunday
					if (key === 'a') {
						return ['日', '一', '二', '三', '四', '五', '六'][value]
					}
					if (result.length > 0 && value < 10) {
						value = '0' + value
					}
					return value || 0
				})
				return timeStr
			}
		}
	}
</script>

<style lang="scss" scoped>
	.el-pagination {
		margin-top: 10px;
	}

	.box-card {
		width: 95%;
		margin: auto;
	}

	.Serach_b {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
	}

	.Serach_b .el-button {
		margin-left: 20px;
	}

	.el-textarea {
		margin-left: 20px;
		width: 20%;
	}
</style>
